<?php

namespace app\common\model;
/**
 * [Order  订单表]
 * @author [C。]
 * @return html
 */
class Order extends Base
{
    protected $name = 'order';
    /**
     * 根据搜索条件获取寺院列表信息
     */
    public function getlist($map,$from = 0, $size = 5,$field='*')
    {
        $order = ['o.id' => 'desc'];
        $join1 = [
            ['app_goods g','g.id = o.goodsid'],
        ];
        $join2 = [
            ['app_goods_option go','go.id = o.optionid'],
        ];
//        $join3 = [
//            ['app_goods_group gg','gg.id = o.groupid'],
//        ];
        $result =  $this->field($field)
            ->alias('o')
            ->join($join1)
            ->join($join2)
//            ->join($join3)
            ->where($map)
            ->order($order)
            ->limit($from, $size)
            ->select();

        return $result;
    }

    /**
     * 搜索获取寺院列表总数
     * @param array $param
     */
    public function getCount($map) {
        $join1 = [
            ['app_goods g','g.id = o.goodsid'],
        ];
        $join2 = [
            ['app_goods_option go','go.id = o.optionid'],
        ];
//        $join3 = [
//            ['app_goods_group gg','gg.id = o.groupid'],
//        ];
        $result =  $this->alias('o')
            ->join($join1)
            ->join($join2)
//            ->join($join3)
            ->where($map)
            ->count();

        return $result;
    }
    /**
     * 根据搜索条件获取寺院列表信息
     */
    public function getOne($map,$field='*')
    {
        $join1 = [
            ['app_goods g','g.id = o.goodsid'],
        ];
        $join2 = [
            ['app_goods_option go','go.id = o.optionid'],
        ];
        $result =  $this->field($field)
            ->alias('o')
            ->join($join1)
            ->join($join2)
            ->where($map)
            ->find();

        return $result;
    }
    /**
     * 根据搜索条件获取寺院列表信息
     */
    public function getlists($memid,$time,$from = 0, $size = 5,$field='*')
    {
        $sql = "SELECT $field FROM `app_order` `o` INNER JOIN `app_goods` `g` ON `g`.`id`=`o`.`goodsid` INNER JOIN `app_goods_option` `go` ON `go`.`id`=`o`.`optionid` WHERE $memid (`o`.`status` = 2) OR (`o`.`status` = 0 AND `o`.`create_time` <= $time) OR (`o`.`refundid` > 0) ORDER BY `o`.`id` DESC LIMIT $from,$size";
        $result =  $this->query($sql);
        return $result;
    }
    /**
     * 搜索获取寺院列表总数
     * @param array $param
     */
    public function getCounts($memid,$time) {
        $sql = "SELECT COUNT(*) AS tp_count FROM `app_order` `o` INNER JOIN `app_goods` `g` ON `g`.`id`=`o`.`goodsid` INNER JOIN `app_goods_option` `go` ON `go`.`id`=`o`.`optionid` WHERE $memid (`o`.`status` = 2) OR (`o`.`status` = 0 AND `o`.`create_time` <= $time) OR (`o`.`refundid` > 0)";
        $result =  $this->query($sql);

        return $result[0]['tp_count'];
    }
    /**
     * 搜索为整团时得团和拼团未付款得团
     * @param array $param
     */
    public function getCount1($goodsid,$gotime,$time) {
        $result =  $this->query("SELECT COUNT(*) AS num from app_order where (status = 1 AND goodsid = $goodsid AND gotime = $gotime AND groupid is null) OR (status = 0 AND create_time > $time AND goodsid = $goodsid AND gotime = $gotime AND groupid is null)");
        return $result;
    }
    /**
     * 搜索为拼团时得团
     * @param array $param
     */
    public function getCount2($goodsid,$gotime) {
        $result =  $this->query("SELECT COUNT(*) AS num from app_order as o,app_goods_group as gg where o.status = 1 AND o.goodsid = $goodsid AND o.gotime = $gotime AND gg.orderid = o.id");
        return $result;
    }
    /**
     * 搜索获取寺院列表总数
     * @param array $param
     */
    public function gethassum($map) {
        $join1 = [
            ['app_goods_group_person gp','gp.orderid = o.id'],
        ];
        $join2 = [
            ['app_goods_group g','g.id = gp.group_id'],
        ];
        $result =  $this->alias('o')
            ->join($join1)
            ->join($join2)
            ->where($map)
            ->sum('o.num');

        return $result;
    }
}